<template>
    <div>
        <el-input v-model="hash"></el-input>
        <el-button type="primary" @click="metamask">
            保存您的IPFS密钥到区块链上!
        </el-button>
    </div>
</template>

<script>
import Web3 from 'web3'
export default {
    data () {
        return {
            total: 1,
            hash: ''
        }
    },
    methods: {
        metamask () {
            const total = this.total
            window.ethereum.enable().then((e) => {
                const address = e[0]
                this.$message.success("成功访问区块链")
                this.$message.success('区块网络' + window.ethereum.networkVersion)
                this.$message.success('区块账户' + window.ethereum.selectedAddress)
                this.$message.success(window.ethereum.isMetaMask ? "metamask已安装" : "metamask未安装")
                const web3 = new Web3(
                    new Web3.providers.HttpProvider("http://localhost:7545")
                );
                console.log(web3);
                const settings = [{
                    "from": address,
                    "to": "0xa87a496Ff059cD56c72763BebB5Ab4F8409B65da",
                    "gas": "3b9aca00",
                    "gasPrice": "3b9aca00",
                    "value": (parseInt(total) * 1000000000000000000).toString(16),
                    "data": this.hash
                }]
                window.ethereum.send({
                    method: 'eth_sendTransaction',
                    params: settings,
                    from: address,
                }, (success, error) => {
                    if (success) {
                        this.$message.success('已确认区块链交易请求', 7)
                    }
                    else { console.log(error); }
                }).then(diff => {
                    console.log(diff);
                })
            });
        }
    }
}
</script>

<style>
</style>